<?php

    if (!defined('BASEPATH'))
        exit('No direct script access allowed');

    /**
     * @author Ivan Kukic
     * @version 2.0
     * @created 09-Oct-2012 19:31:24
     */
    class translate {

        public function __construct() {
            $this->CI = &get_instance();

            if (!isset($this->CI->languages))
                $this->CI->load->config('languages');

            // load colparse
            $this->CI->colparse = & $this->CI->factory->create('colparse', 'foo', 'bar');
        }

        /**
         *
         * @param arr
         * @param lang
         * @return array
         */
        function asoc_array($arr, $lang) {
            foreach ($arr as $key => $value):

                // we go recursive if there is sub array in associative array
                if (is_array($value)) {
                    $arr[$key] = $this->res_array($value, $lang);
                } else {
                    if (substr($value, 0, 4) == '<!--'):
                        $arr[$key] = $this->CI->strparse->find_lang_in_string($arr[$key], $lang);
                    endif;
                }

            endforeach;

            return $arr;
        }

        /**
         *
         * @param arr
         * @param lang
         * @return array
         */
        function clean_asoc_array($arr, $lang) {

            if (count($arr) == 0)
                return array();

            foreach ($arr as $key => $value):

                // we go recursive if there is sub array in associative array
                if (is_array($value)) {
                    $arr[$key] = $this->clean_asoc_array($value, $lang);
                } else {
                    if (substr($value, 0, 4) == '<!--'):
                        $arr[$key] = $this->CI->strparse->find_lang_in_string($arr[$key], $lang);
                    endif;
                }

            endforeach;

            return $arr;
        }

        /**
         *
         * @param arr
         * @param lang
         * @return array
         */
        function res_array($arr, $lang) {

            if (empty($arr))
                return array();

            foreach ($arr as $key => $value):

                $arr[$key] = $this->asoc_array($value, $lang);

            endforeach;

            return $arr;
        }

        /**
         *
         * @param arr
         * @return array
         */
        function parse_asoc($post) {

            foreach ($post as $key => $value):
                if (is_array($value))
                    $post[$key] = $this->CI->colparse->regular_array_to_col($value);

            endforeach;

            return $post;
        }

        /**
         *
         * @param $post
         * @param $count_post
         * @return array
         */
        function parse_multiple($post, $count_post) {
            $description_array = array();

            for ($i = 0, $j = 0; $i < $count_post; $i++):
                $language_string = "";
                foreach (config_item('languages') as $key => $value):
                    $language_string .= $this->CI->strparse->create_lang_string($post[$j], $key);
                    $j++;
                endforeach;
                $description_array[] = $language_string;
            endfor;

            return $description_array;
        }

    }

?>